/**
 * @Author: wzx
 * @Description:
 * @Version: 1.0.0
 * @Date: 2021/5/20 上午1:14
 *@Copyright: MIN-Group；国家重大科技基础设施——未来网络北大实验室；深圳市信息论与未来网络重点实验室
 */
package testrouter

import (
	"context"
	"encoding/json"
	"vpn-management-server/logger"
	"vpn-management-server/network"
	"vpn-management-server/security/code"
)

func TestRouter(ctx context.Context, request *network.NetRequest, response *network.NetResponse) {
	if request.Command == "" {
		buf, _ := json.Marshal(request)
		logger.Logger.ErrorfWithIdAndConn(ctx.Value("conn_id").(string), request.GetRemote(), "Error the Request Command is Null, "+string(buf))
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "Error the Request Command is Null"
	} else {
		if f, ok := testRouter.HandleSet[request.Command]; !ok {
			buf, _ := json.Marshal(request)
			logger.Logger.ErrorfWithIdAndConn(ctx.Value("conn_id").(string), request.GetRemote(), "Not Supported the Request Command, "+string(buf))
			response.Code = code.BAD_REQUEST
			response.ErrorMsg = "Not Supported the Request Command"
		} else {
			isCon := true
			if af, ok := testRouter.BeforeHandleSet[request.Command]; ok {
				isCon = af(ctx, request, response)
			}
			if isCon == true {
				f(ctx, request, response)
				if bf, ok := testRouter.AfterHandleSet[request.Command]; ok {
					bf(ctx, request, response)
				}
			}
		}
	}
}
